// pages/property/wyfList/index.js
var app = getApp(), fetchApi = require("../../../utils/httpPost.js"), common_Js = require("../../../component/common.js")
import Dialog from '../../../component/vant/dialog/dialog';
import Toast from '../../../component/vant/toast/toast';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    wyfList:[],
    isLoading:true,
    room_id:0,
    hasData:false,
    room:{},
    checkIndexs:[],
    yearNum:'',
    totalPrice:0,
    totalPriceYuan:0,
    checkIds:[],
    activeNames:[],
    isShowPopup:false,
    beginMonth:'',
    endMonth:'',
    currentDate:new Date(2022, 0, 1),
    minDate: new Date(2022, 0, 1).getTime(),
    dateType:0,
    chooseTotalCount:0,
    checkAll:false,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    if(options.room_id){
      this.data.room_id = parseInt(options.room_id);
    }

    var date = new Date();
    var year = date.getFullYear();

    this.data.beginMonth = year +'-01';
    this.data.endMonth = year +'-12';

    
    this.setData({
      beginMonth: this.data.beginMonth,
      endMonth: this.data.endMonth,
    })

    this.onHttpInit();
  },
  /**
   * 获取页面数据的 http请求
   */
  onHttpInit: function () {
    var that = this
    fetchApi.httpPost('/estate/fee/order/list', {
      room_id: that.data.room_id,
      begin_month: that.data.beginMonth,
      end_month: that.data.endMonth,
    }, function (res) {
      console.log("我绑定的房间列表", res)
      if (res.data.code == 0) {
        that.setData({
          hasData:true,
          room: res.data.data.room,
          wyfList: res.data.data.all_templates,
        })
        that.initActiveName(res.data.data.all_templates)
      }
      that.setData({
         isLoading: false, loading: true, netWork: true,
      })
    }, function (res) {
        that.setData({ hasData: false,  isLoading: false,  loading: true, netWork: true })
    });
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  initActiveName(templates){

    for(var index in templates){
      this.data.activeNames.push( (parseInt(index+'') + 1) + '');
    }
    console.log('this.data.activeNames',this.data.activeNames)
    this.setData({
      activeNames: this.data.activeNames
    })
  },
  onChangeCollapse(e){
    this.setData({
      activeNames: e.detail,
    });
  },
  onChangeDatePicker(e){
    var type = e.currentTarget.dataset.type;
    this.setData({
      isShowPopup: !this.data.isShowPopup,
    })
    
    if(type == 'begin'){
      this.data.dateType = 0;
      var arrDate = this.data.beginMonth.split("-");
      var time = new Date(arrDate[0],parseInt(arrDate[1]) - 1,1).getTime();

      this.setData({
        currentDate: time
      })
    }else{
      this.data.dateType = 1;
      var arrDate = this.data.endMonth.split("-");
      var time = new Date(arrDate[0],parseInt(arrDate[1]) - 1,1).getTime();

      this.setData({
        currentDate: time
      })
    }
  },
  onConfirmDate(e){
    console.log('onConfirmDate',e)
    var time = new Date(e.detail);
    var month = time.getMonth() + 1;
    var date = time.getFullYear() + '-' + (month < 10 ? '0' + month : month );
    console.log('date',date)

    if(this.data.dateType == 0){
      this.data.beginMonth = date;
      this.setData({
        beginMonth: date
      })
    } else if(this.data.dateType == 1){
      this.data.endMonth = date;
      this.setData({
        endMonth: date
      })
    }
    this.setData({
      isShowPopup:!this.data.isShowPopup
    })
    this.onHttpInit();
  },
  onCancelDate(e){
    console.log('onCancelDate',e)
    this.setData({
      isShowPopup:!this.data.isShowPopup
    })
  },
  onChangeCheckbox(e){
    console.log('onChangeCheckbox',e);
    this.data.checkIds = e.detail;
    this.setData({
      checkIds: e.detail
    })
    var totalPrice = 0;
    var state = true;
    var totalCount = 0;
    var chooseCount = 0;
    var wyfList = this.data.wyfList;
    //修改状态
    for(var mIndex in wyfList){
      var wyf = wyfList[mIndex];
    //   console.log('wyf',wyf)
      var count = 0;//当前月份选中的项数量

      for(var index in wyf.templates){
        var template = wyf.templates[index];
        // console.log('template',template)
        totalCount++;
        if(template.state == 0){//只有在未支付的情况下才处理 
          var tId = template.id;
          for(var sIndex in this.data.checkIds){
            if(tId == this.data.checkIds[sIndex]){
              wyf.templates[index].isCheck = 1;
              totalPrice += template.totalPrice;
              
              //物业费 看 垃圾处理费是否有
              if(template.feeType == 'wyf'){
                console.log('template.rubbish',template.rubbish)
                if(template.rubbish!=undefined){
                  if(template.rubbish.totalPrice > 0){
                    totalPrice += template.rubbish.totalPrice;
                  }
                }
              } else if(template.feeType == 'water'){
                console.log('template.jiaya',template.jiaya)
                if(template.jiaya!=undefined){
                  if(template.jiaya.totalPrice > 0){
                    totalPrice += template.jiaya.totalPrice;
                  }
                }
              }

              count ++;
              chooseCount++;
              break;
            }
          }
        }
      }
      wyf.chooseCount = count;
      this.data.wyfList[mIndex] = wyf;
    }
    if(totalCount == chooseCount){
      state = true;
    }else{
      state = false;
    }
    this.data.checkAll = state;

    this.data.wyfList = wyfList;

    this.data.totalPriceYuan = totalPrice;
    this.setData({
      chooseTotalCount:chooseCount,
      checkAll: state,
      totalPrice: totalPrice * 100,
      wyfList:this.data.wyfList,
    })

  },
  onChangeCheckboxNew(e){
    console.log('onChangeCheckbox',e);
    this.data.checkIds = e.detail;

    var lastId = this.data.checkIds[this.data.checkIds.length - 1];
    console.log('lastId',lastId)




    var totalPrice = 0;
    var state = true;
    var totalCount = 0;
    var chooseCount = 0;
    var wyfList = this.data.wyfList;

    //修改状态
    for(var mIndex in wyfList){
      var wyf = wyfList[mIndex];
    //   console.log('wyf',wyf)
      var count = 0;//当前月份选中的项数量

      for(var index in wyf.templates){
        var template = wyf.templates[index];
        // console.log('template',template)
        totalCount++;
        if(template.state == 0){//只有在未支付的情况下才处理 
          var tId = template.id;
          for(var sIndex in this.data.checkIds){
            if(tId == this.data.checkIds[sIndex]){
              wyf.templates[index].isCheck = 1;
              totalPrice += template.totalPrice;
              
              //物业费 看 垃圾处理费是否有
              if(template.feeType == 'wyf'){
                console.log('template.rubbish',template.rubbish)
                if(template.rubbish!=undefined){
                  if(template.rubbish.totalPrice > 0){
                    totalPrice += template.rubbish.totalPrice;
                  }
                }
              } else if(template.feeType == 'water'){
                console.log('template.jiaya',template.jiaya)
                if(template.jiaya!=undefined){
                  if(template.jiaya.totalPrice > 0){
                    totalPrice += template.jiaya.totalPrice;
                  }
                }
              }

              count ++;
              chooseCount++;
              break;
            }
          }
        }
      }
      wyf.chooseCount = count;
      this.data.wyfList[mIndex] = wyf;
    }
    if(totalCount == chooseCount){
      state = true;
    }else{
      state = false;
    }
    this.data.checkAll = state;

    this.data.wyfList = wyfList;

    this.data.totalPriceYuan = totalPrice;
    this.setData({
      chooseTotalCount:chooseCount,
      checkAll: state,
      totalPrice: totalPrice * 100,
      wyfList:this.data.wyfList,
      checkIds: e.detail
    })

  },
  /**
   *  全选
   */
  onChangeCheckAll(e){
    console.log('onChangeCheckAll',e)
    var state = e.detail;

    var totalPrice = 0;
    var chooseIds = [];
    var totalCount = 0;
    var chooseCount = 0;
    var checkIds = [];
    var wyfList = this.data.wyfList;
    //修改状态
    for(var mIndex in wyfList){
      var wyf =  wyfList[mIndex];
      console.log('wyf',wyf)
      var count = 0;//当前月份选中的项数量

      for(var index in wyf.templates){
        var template = wyf.templates[index];
        totalCount++;
        if(template.state == 0){//只有在未支付的情况下才处理 
          if(state){
              wyf.templates[index].isCheck = 1;
              totalPrice += template.totalPrice;
              
              //物业费 看 垃圾处理费是否有
              if(template.feeType == 'wyf'){
                console.log('template.rubbish',template.rubbish)
                if(template.rubbish!=undefined){
                  if(template.rubbish.totalPrice > 0){
                    totalPrice += template.rubbish.totalPrice;
                  }
                }
              } else if(template.feeType == 'water'){
                console.log('template.jiaya',template.jiaya)
                if(template.jiaya!=undefined){
                  if(template.jiaya.totalPrice > 0){
                    totalPrice += template.jiaya.totalPrice;
                  }
                }
              }

              count ++;
              chooseCount++;
              checkIds.push(template.id+'');
          }else{
              wyf.templates[index].isCheck = 0;
          }
        }
      }
      wyf.chooseCount = count;
      wyfList[mIndex] = wyf;
      if(state){
        var name = parseInt(mIndex+'')+ 1
        chooseIds.push(name+'');
      }
    }
    this.data.totalPriceYuan = totalPrice;
    this.data.checkAll = state;
    this.data.wyfList = wyfList;
    console.log("chooseIds",chooseIds)
    console.log("checkIds",checkIds)
    this.data.activeNames = chooseIds;
    this.setData({
      chooseTotalCount: chooseCount,
      checkAll: state,
      checkIds:checkIds,
      totalPrice: totalPrice * 100,
      wyfList: this.data.wyfList,
      activeNames: chooseIds,
    })
  },
  /**
   *  一键选择
   */
  handleOneKey(e){
    var type = e.currentTarget.dataset.type;
    var state = true;
    var chooseIds = [];
    var totalCount = 0;
    // var chooseCount = 0;
    
    var wyfList = this.data.wyfList;
    //修改状态
    for(var mIndex in wyfList){
      var wyf = wyfList[mIndex];
      console.log('wyf',wyf)
      for(var index in wyf.templates){
        var template = wyf.templates[index];
        totalCount++;
        if(template.state == 0){//只有在未支付的情况下才处理 
          if(wyf.templates[index].feeType == type){
            if(wyf.templates[index].isCheck == 0){
              wyf.templates[index].isCheck = 1;
              this.data.totalPriceYuan += template.totalPrice;

              //物业费 看 垃圾处理费是否有
              if(template.feeType == 'wyf'){
                console.log('template.rubbish',template.rubbish)
                if(template.rubbish!=undefined){
                  if(template.rubbish.totalPrice > 0){
                    this.data.totalPriceYuan += template.rubbish.totalPrice;
                  }
                }
              } else if(template.feeType == 'water'){
                console.log('template.jiaya',template.jiaya)
                if(template.jiaya!=undefined){
                  if(template.jiaya.totalPrice > 0){
                    this.data.totalPriceYuan += template.jiaya.totalPrice;
                  }
                }
              }

              wyf.chooseCount ++;
              this.data.chooseTotalCount++;
              if(this.data.checkIds.indexOf(template.id+'') < 0){
                this.data.checkIds.push(template.id+'');
              }

            }
          }
        }
      }
      wyfList[mIndex] = wyf;
      if(wyf.chooseCount > 0){
        var name = parseInt(mIndex+'')+ 1
        if(this.data.activeNames.indexOf(name) < 0){
          this.data.activeNames.push(name+'');
        }
      }
    }
    this.data.wyfList = wyfList;
    
    if(totalCount == this.data.chooseTotalCount){
      state = true;
    }else{
      state = false;
    }
    this.data.checkAll = state;

    console.log(" this.data.activeNames", this.data.activeNames)
    // console.log("checkIds",checkIds)

    this.setData({
      chooseTotalCount: this.data.chooseTotalCount,
      checkAll: state,
      checkIds:this.data.checkIds,
      totalPrice: this.data.totalPriceYuan * 100,
      wyfList: this.data.wyfList,
      activeNames: this.data.activeNames,
    })

  },
  submitOrder:function(e){
    var that = this;

    if(this.data.checkIds.length == 0){
      Toast.fail('请选择缴费项目');
      return;
    }
    //将选中的id 的数组 转换成 拼接字符串  逗号分隔
    var ids = this.data.checkIds.join();


    that.setData({ submitting : true})
    fetchApi.httpPost('/property/addOrderByTemplate', {
      room_id: that.data.room_id,
      begin_month: that.data.beginMonth,
      end_month: that.data.endMonth,
      checkIds: ids,
    }, function (res) {
      console.log('res', res)
      if (res.data.code == 0) {
        var order_id = res.data.data;
        console.log('order_id',order_id);
        wx.navigateTo({
          url: '/pagesProperty/pages/propertySettle/index?is_reg=0&order_id='+order_id,
        })
      } else {
        Toast.fail(res.data.msg);
      }
    },function(){})
  },
  
  /**
   * 生命周期函数--监听页面显示
   */
  onShow() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh() {
    this.onHttpInit();
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom() {

  },
})